package org.x.user.controller;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.x.common.config.Result;
import org.x.common.enums.LogAsync;
import org.x.user.dto.AssignAclsDTO;
import org.x.user.dto.AssignRolesDTO;
import org.x.user.dto.DataScopeDTO;
import org.x.user.dto.LoginDTO;
import org.x.user.service.*;
import org.x.user.service.impl.SysRoleUserServiceImpl;
import org.x.user.vo.LoginVO;
import org.x.user.vo.UserInfoVO;

import java.util.List;

@RestController
@RequestMapping("/api/auth")
public class SysLoginController {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    ISysRoleUserService sysRoleUserService;
    @Autowired
    ISysRoleAclService sysRoleAclService;
    @Autowired
    ISysRoleDeptService sysRoleDeptService;

    @PostMapping("/login")
    public Result login(@RequestBody @Valid LoginDTO loginDTO) {
        LoginVO login = sysUserService.login(loginDTO);
        return Result.ok(login);
    }

    @GetMapping("/info")
    public Result<UserInfoVO> userInfo() {
        return Result.ok(sysUserService.getUserAuthInfo());
    }

    //查询用户权限信息
    @GetMapping("/useracl")
    public Result<List<String>> getPermissions() {
        return Result.ok(sysUserService.getUserAuthInfo().getPermissions());
    }

    //--------------------授权管理------------------

    @LogAsync("用户角色分配")
    @PostMapping("/assignRoles")
    public Result assignRoles(@RequestBody @Valid AssignRolesDTO assignRolesDTO) {
        return Result.ok(sysRoleUserService.assignRoles(assignRolesDTO));
    }

    @LogAsync("角色权限分配")
    @PostMapping("/assignAcls")
    public Result assignAcls(@RequestBody @Valid AssignAclsDTO assignAclsDTO) {
        return Result.ok(sysRoleAclService.assignAcls(assignAclsDTO));
    }

    @LogAsync("数据权限配置")
    @PostMapping("/dataScope")
    public Result dataScope(@RequestBody @Valid DataScopeDTO dataScopeDTO) {
        return Result.ok(sysRoleDeptService.dataScope(dataScopeDTO));
    }

    @LogAsync("用户权限查询")
    @GetMapping("/userPermissions/{id}")
    public Result userPermissions( @PathVariable Long id) {
        return Result.ok(sysUserService.userPermissions(id));
    }

}
